<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      
            <el-form-item label="填报人" prop="fdName">
          <el-input
            v-model="queryParams.fdName"
            placeholder="请输入填报人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

      <el-form-item label="年份" prop="years">
            <el-select v-model="queryParams.years" placeholder="请选择年份" clearable>
              <el-option
                v-for="dict in dict.type.sys_teacher_kpi_filling_year"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
      
      <el-form-item label="任务描述" prop="taskDescribe">
        <el-input
          v-model="queryParams.taskDescribe"
          placeholder="请输入任务描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="奖励人" prop="prizes">
        <el-input
          v-model="queryParams.prizes"
          placeholder="请输入奖励人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:task:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teacher:task:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teacher:task:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:task:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      
      <el-table-column label="填报人" align="center" prop="fdName" />
      <el-table-column label="年份" align="center" prop="years">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_teacher_kpi_filling_year" :value="scope.row.years"/>
            </template>
          </el-table-column>

      <!-- <el-table-column label="任务描述" align="center" prop="taskDescribe" /> -->
       <el-table-column label="任务描述" align="center" prop="taskDescribe" >
                          <template slot-scope="scope">
                                  
                              <div :title="scope.row.taskDescribe" style="white-space:nowrap;width:100%;text-overflow: ellipsis;overflow: hidden;">{{ scope.row.taskDescribe }}</div>
                            </template>
                        </el-table-column>
      <el-table-column label="开始时间" align="center" prop="taskStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.taskStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="taskEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.taskEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="奖励人" align="center" prop="prizes" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teacher:task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teacher:task:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改综合测试-最大奖励对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="填报人" prop="fdName" :rules="rules.reason1">
          <el-input v-model="form.fdName" placeholder="请输入填报人" />
        </el-form-item>

        <el-form-item label="年份" prop="years" :rules="rules.reason2">
                  <el-select v-model="form.years" placeholder="请选择年份">
                    <el-option
                      v-for="dict in dict.type.sys_teacher_kpi_filling_year"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
        </el-form-item>

        <el-form-item label="任务描述" prop="taskDescribe" :rules="rules.reason3">
          <el-input v-model="form.taskDescribe" placeholder="请输入任务描述" />
        </el-form-item>
         <el-form-item label="开始时间" prop="taskStart" :rules="rules.reason4">
          <el-date-picker clearable
            v-model="form.taskStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="taskEnd" :rules="rules.reason4">
          <el-date-picker clearable
            v-model="form.taskEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="奖励人" prop="prizes" :rules="rules.reason5">
          <el-input v-model="form.prizes" placeholder="请输入奖励人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/teacher/task";
import { getUserProfile } from "@/api/system/user"; // 获取当前用户接口
export default {
  name: "Task",
  dicts: ['rt_filling_college', 'sys_commit_status', 'sys_teacher_kpi_filling_year'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 综合测试-最大奖励表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskDescribe: null,
        taskTime: null,
        prizes: null,
        years: null,
        fdName: null,
        taskStart: null,
        taskEnd: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reason1: [{ required: true, message: "填报人不能为空", trigger: "blur" }], 
        reason2: [{ required: true, message: "年份不能为空", trigger: "blur" }], 
        reason3: [{ required: true, message: "任务描述不能为空", trigger: "blur" }], 
        reason4: [{ required: true, message: "时间范围不能为空", trigger: "blur" }], 
        reason5: [{ required: true, message: "奖励人不能为空", trigger: "blur" }],
            
      }
    };
  },
  created() {
    // this.getList();
     this.getUser();
  },
  methods: {        // 获取当前登录信息
    getUser() {
      this.loading = true;
      getUserProfile().then(response => {
        // this.user = response.data;
        this.roleGroup = response.roleGroup;
        // this.postGroup = response.postGroup;
        // console.log(response);
        if (response.roleGroup.includes('辅导员')) {
          this.queryParams.fdName = response.data.nickName
          this.getList();
          return
        } else if (!response.roleGroup.includes('辅导员')) {
          this.getList();
          return
        }
        this.loading = false;
        // this.getList();
      });
    },
    /** 查询综合测试-最大奖励列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskDescribe: null,
        taskTime: null,
        prizes: null,
        years: null,
        fdName: null,
        taskStart: null,
        taskEnd: null,

        
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

           // 检查表单中是否有空字符串
           let hasEmptyString = Object.values(this.form).some(value => {
            return (value !== null && value !== undefined && (typeof value === 'string' ? value.trim() === '' : false));
          });

          if (hasEmptyString) {
            this.$modal.msgError("表单中存在空字符串，请填写完整");
            return;
          }

          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
      // .confirm('是否确认删除综合测试-最大奖励编号为"' + ids + '"的数据项？')
      .confirm('是否确认删除？')
      .then(function() {
        return delTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
